package com.workManager.controller;

import com.github.pagehelper.PageInfo;
import com.wordnik.swagger.annotations.ApiOperation;
import com.workManager.base.BaseController;
import com.workManager.config.RespCode;
import com.workManager.pojo.TbProject;
import com.workManager.pojo.TbProjectPayment;
import com.workManager.service.ProjectService;
import com.workManager.util.WebUtil;
import com.workManager.vo.ProjectPaymentVo;
import com.workManager.vo.ProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Thinkpad on 2017/1/16 0016.
 */
@Controller
@RequestMapping("/project")
public class ProjectController extends BaseController{

    @Autowired
    private ProjectService projectService;




    @ApiOperation(value ="项目添加",notes = "项目添加")
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Object add(@RequestBody TbProject project){
        ModelMap modelMap = new ModelMap();
        project.setCreateDate(new Date());
        project.setIsValid(true);
        try{
            projectService.add(project);
        }catch (DuplicateKeyException e){
            if(e.toString().indexOf("for key 'PROJECT_NAME'")>0){
                return setModelMap(modelMap, RespCode.INTERNAL_SERVER_ERROR, "项目名称重复");
            }else{
                return setModelMap(modelMap, RespCode.INTERNAL_SERVER_ERROR, "项目编号重复");
            }

        }
        return setSuccessModelMap(modelMap,"保存成功");
    }

    @ApiOperation(value ="删除项目",notes = "删除项目")
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public Object delete(HttpServletRequest request,@RequestParam(value = "projectIds[]") List<String> projectIds){
        ModelMap modelMap = new ModelMap();
        System.out.println("-------------------------");
       for (String s : projectIds){
           System.out.println("-------"+s);
       }
        String[] ids = new String[projectIds.size()];
        for(int i =0;i<projectIds.size();i++){
            ids[i] = projectIds.get(i);
        }
        try {
            projectService.deleteByProjectIds(ids);
        }catch(DataIntegrityViolationException e){
            return setModelMap(modelMap,RespCode.INTERNAL_SERVER_ERROR,"不能删除有关联项目");
        }
        return setSuccessModelMap(modelMap);
    }

    @ApiOperation(value ="更新项目",notes = "更新项目")
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Object update(HttpServletRequest request,@RequestBody TbProject project){
        ModelMap modelMap = new ModelMap();
        project.setCreateDate(new Date());
        project.setIsValid(true);
        try {
            projectService.update(project);
        }catch (DuplicateKeyException e){
            return setModelMap(modelMap,RespCode.INTERNAL_SERVER_ERROR,"项目名:"+project.getProjectName()+" 或项目编号："+project.getProjectCode()+" 已存在！");
        }

        return setSuccessModelMap(modelMap,"保存成功");
    }

    @ApiOperation(value ="项目查询",notes = "项目查询")
    @RequestMapping(value = "/query",method = RequestMethod.POST)
    public Object query(@RequestParam(value = "pageNumber",required = false) String pageNumber,
                        @RequestParam(value="pageSize", required = false) String pageSize,
                        @RequestParam(value="projectId", required = false) String projectId,
                        @RequestParam(value="projectCode", required = false) String projectCode,
                        @RequestParam(value="projectName", required = false) String projectName,
                        @RequestParam(value="startTime", required =  false) String startTime,
                        @RequestParam(value="endTime", required =  false) String endTime,
                        @RequestParam(value="projectType", required = false) String projectType,
                        @RequestParam(value="projectManager", required = false) String projectManager,
                        @RequestParam(value="projectManagerName", required = false) String projectManagerName,
                        @RequestParam(value="saler", required = false) String saler,
                        @RequestParam(value="salerName", required = false) String salerName,
                        @RequestParam(value="projectStatus",required = false) String projectStatus,
                        @RequestParam(value="loginCode",required = false) String loginCode){


        //String loginCode = WebUtil.getCurrentUserVO().getLoginCode();
        ModelMap modelMap = new ModelMap();
        Integer pagenum = pageNumber==null?1:Integer.parseInt(pageNumber);
        Integer pagesize = pageSize==null?10:Integer.parseInt(pageSize);
        Map<String,Object> dataMap = new HashMap<String, Object>();
        Map<String,Object> map = new HashMap();
        map.put("pagenum",pagenum);
        map.put("pagesize",pagesize);


        map.put("projectId",projectId);
        map.put("projectCode",projectCode);
        map.put("projectName",projectName);
        map.put("startTime",startTime);
        map.put("endTime",endTime);
        map.put("projectType",projectType);
        map.put("projectManager",projectManager);
        map.put("projectManagerName",projectManagerName);
        map.put("saler",saler);
        map.put("salerName",salerName);
        map.put("projectStatus",projectStatus);
        map.put("loginCode",loginCode);


        PageInfo<ProjectVo> projectPageInfo = null;
        try {
            projectPageInfo = projectService.query(map);
        }catch (Exception e){
            e.printStackTrace();
            String msg = e.getMessage();
            System.out.println(msg);
        }

        if(projectPageInfo!=null){
            dataMap.put("total",projectPageInfo.getTotal());
            dataMap.put("rows",projectPageInfo.getList());
        }
        return setSuccessModelMap(modelMap,dataMap);
    }

    @ApiOperation(value ="根据项目状态查询项目",notes = "根据项目状态查询项目")
    @RequestMapping(value = "/queryByProjectStatus",method = RequestMethod.POST)
    public Object queryByProjectStatus(HttpServletRequest request,@RequestParam(value = "pageNumber",required = false) String pageNumber,
                                       @RequestParam(value="pageSize", required = false) String pageSize,
                                       @RequestParam(value="projectStatus", required = true) String projectStatus){
        ModelMap modelMap = new ModelMap();
        Integer pagenum = pageNumber==null?1:Integer.parseInt(pageNumber);
        Integer pagesize = pageSize==null?10:Integer.parseInt(pageSize);
        Map<String,Object> dataMap = new HashMap<String, Object>();
        Map<String,Object> map = new HashMap();
        map.put("pagenum",pagenum);
        map.put("pagesize",pagesize);
        map.put("projectStatus",projectStatus);
        PageInfo<TbProject> projectPageInfo = projectService.queryByProjectStatus(map);
        dataMap.put("total",projectPageInfo.getTotal());
        dataMap.put("rows",projectPageInfo.getList());
        return  setSuccessModelMap(modelMap,dataMap);

    }

    @ApiOperation(value ="项目收款添加",notes = "添加项目收款")
    @RequestMapping(value = "/insertPayment",method = RequestMethod.POST)
    public Object insertPayment(HttpServletRequest request, @RequestBody TbProjectPayment projectPayment){
        ModelMap modelMap = new ModelMap();
        projectPayment.setCreateDate(new Date());
        projectPayment.setIsValid(true);
        projectService.insertPayment(projectPayment);
        return  setSuccessModelMap(modelMap,"保存成功");
    }

    @ApiOperation(value ="项目收款更新",notes = "项目收款更新")
    @RequestMapping(value = "/updatePayment",method = RequestMethod.POST)
    public Object updatePayment(HttpServletRequest request, @RequestBody TbProjectPayment projectPayment){
        ModelMap modelMap = new ModelMap();
        projectPayment.setIsValid(true);
        projectService.updatePayment(projectPayment);
        return  setSuccessModelMap(modelMap,"保存成功");
    }

    @ApiOperation(value ="项目收款删除",notes = "项目收款删除")
    @RequestMapping(value = "/deletePayment",method = RequestMethod.POST)
    public Object deletePayment(@RequestParam(value = "Ids[]") List<Integer> Ids){
        ModelMap modelMap = new ModelMap();
        projectService.delete(Ids);
        return  setSuccessModelMap(modelMap,"删除成功");
    }

    @ApiOperation(value ="项目收款调整分摊比例",notes = "项目收款调整分摊比例")
    @RequestMapping(value = "/changeRatio",method = RequestMethod.POST)
    public Object changeRatio(@RequestParam(value = "Ids[]") List<Integer> Ids,@RequestParam(value = "ratio") String ratio){
        ModelMap modelMap = new ModelMap();
        projectService.updatePaymentRatio(Ids,ratio);
        return  setSuccessModelMap(modelMap,"保存成功");
    }

    @ApiOperation(value ="项目收款查询",notes = "项目收款查询")
    @RequestMapping(value = "/queryPayment",method = RequestMethod.POST)
    public Object queryPayment(HttpServletRequest request,@RequestParam(value = "pageNumber",required = false) String pageNumber,
                               @RequestParam(value="pageSize", required = false) String pageSize,
                               @RequestParam(value = "paymentId",required = false) String paymentId,
                               @RequestParam(value = "projectId",required = false) String projectId,
                               @RequestParam(value = "projectCode",required = false) String projectCode,
                               @RequestParam(value = "projectName",required = false) String projectName,
                               @RequestParam(value = "allocationType",required = false) String allocationType,
                               @RequestParam(value = "paymentType",required = false) String paymentType,
                               @RequestParam(value = "billingDateFrom",required = false) String billingDateFrom,
                               @RequestParam(value = "billingDateTo",required = false) String billingDateTo,
                               @RequestParam(value = "realPaymentDateFrom",required = false) String realPaymentDateFrom,
                               @RequestParam(value = "realPaymentDateTo",required = false) String realPaymentDateTo,
                               @RequestParam(value = "loginCode", required = false) String loginCode
    ){
        ModelMap modelMap = new ModelMap();
        Integer pagenum = pageNumber==null?1:Integer.parseInt(pageNumber);
        Integer pagesize = pageSize==null?10:Integer.parseInt(pageSize);
        Map<String,Object> dataMap = new HashMap<String, Object>();
        Map<String,Object> map = new HashMap();
        map.put("loginCode", loginCode);
        map.put("pagenum",pagenum);
        map.put("pagesize",pagesize);
        map.put("paymentId",paymentId);
        map.put("projectId",projectId);
        map.put("projectCode",projectCode);
        map.put("projectName",projectName);
        map.put("allocationType",allocationType);
        map.put("paymentType",paymentType);
        map.put("billingDateFrom",billingDateFrom);
        map.put("billingDateTo",billingDateTo);
        map.put("realPaymentDateFrom",realPaymentDateFrom);
        map.put("realPaymentDateTo",realPaymentDateTo);

        PageInfo<ProjectPaymentVo> paymentList = projectService.queryPayment(map);
        dataMap.put("total",paymentList.getTotal());
        dataMap.put("rows",paymentList.getList());
        return  setSuccessModelMap(modelMap,dataMap);
    }

    @ApiOperation(value ="按ID查询项目收款信息",notes = "按ID查询项目收款信息")
    @RequestMapping(value = "/queryPaymentById",method = RequestMethod.POST)
    public Object queryPaymentById(@RequestParam(value = "paymentId",required = false) Integer paymentId){
        ModelMap modelMap = new ModelMap();
        TbProjectPayment payment = projectService.queryPaymentById(paymentId);
        return  setSuccessModelMap(modelMap,payment);
    }

    @ApiOperation(value ="获取所有有效项目",notes = "获取所有有效项目")
    @RequestMapping(value = "/getAllProjects",method = RequestMethod.POST)
    public Object getAllProjects(){
        ModelMap modelMap = new ModelMap();
        return  setSuccessModelMap(modelMap,projectService.getAllProjects());
    }



   /* *//**
     * 内部方法：获取前台参数
     * @return Map<String,Object>
     *//*
    private Map<String,Object> buildParameter(HttpServletRequest request){
        Map<String, Object> parameter = new HashMap<String, Object>();
        java.util.Enumeration<String> paremEnu = null;
        paremEnu = request.getParameterNames();
        while (paremEnu.hasMoreElements()) {
            String paramName = paremEnu.nextElement();
            parameter.put(paramName, request.getParameter(paramName));
        }
        return parameter;
    }*/







}
